const express = require("express");
const router = express.Router();
const pool = require("../../../src/pg/config/pg");
const axios = require("axios");
axios.default.timeout = 5000;
const baserulr =
  "http://144.126.242.168:31403/.well-known/stellar-history.json";
//从数据库中小宝这个表中的数据进行处理然后写入表中
router.get("/tl", (req, res) => {
  pool.query("SELECT id , cloud FROM schg", (err, resq) => {
    //获取表中的数据失败处理方案
    if (err) {

      console.log("获取表中数据失败")
    }
    //查询表中的所有的数据
    const data = resq.rows;

    //对数据进行排序 从小到大排序
    data.sort((a, b) => {
      return a.id - b.id;
    });
    //数组数据行遍历处理
    data.map((item) => {
      //获取表中的字段名字
      const id = item.id;
      const cloud = item.cloud;

      //对数据进行处理
      //获取官方区块高度
      axios
        .get(baserulr)
        .then((resq) => {
          //获取表中的区块高度
          axios
            .get(cloud)
            .then(async (resqz) => {
              //表中的区块高度处理方案
              const c = resq.data.currentLedger - resqz.data.core_latest_ledger;
             
              //正常规则
              const z = c < 0 || c < 1000;
              //延迟规则
              const y = c > 1000;
              //新作规则
              const x = resq.data.currentLedger === 1;
//正常规则
              if (z) {
                pool.query(
                  "update schg set  lenger = $1 , status = $2, port=$3 where id = $4 ;",
                  [resqz.data.core_latest_ledger, "正常节点", "开启", id],
                  (err) => {
                    if (err) {
                      console.log(id, "正常节点出错");
                    }
                  }
                );
                pool.end;
              }
                //延迟规则
              if (y) {
                pool.query(
                  "update schg set  lenger = $1 , status = $2, port=$3 where id = $4 ;",
                  [resqz.data.core_latest_ledger, "延迟节点", "开启", id],
                  (err) => {
                    if (err) {
                      console.log(id, "延迟节点出错");
                    }
                  }
                );
                pool.end;
              }
                //新作规则
              if (x) {
                pool.query(
                  "update schg set  lenger = $1 , status = $2, port=$3 where id = $4 ;",
                  [1, "新作节点", "开启", id],
                  (err) => {
                    if (err) {
                      console.log(id, "新作节点出错");
                    }
                  }
                );
                pool.end;
              }
            })
            .catch((err) => {
              //表中获取区块高度失败方案

              pool.query(
                "update schg set  lenger = $1 , status = $2, port=$3 where id = $4 ;",
                [0, "掉线节点", "关闭", id],
                (err) => {
                  if (err) {
                    console.log(id, err.severity);
                  }
                }
              );
            });
        })
        .catch((err) => {
          //官方区块高度错误处理的方案
          console.log("官方区块高度错误");
        });
    });
  });
});

module.exports = router;
